package com.worldtolearn.order.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import com.worldtolearn.order.bu.OrderBu;
import com.worldtolearn.order.entity.Order;
import com.worldtolearn.order.entity.OrderLine;
import com.worldtolearn.user.bean.LoginMB;

@Controller
@Named
@ManagedBean
@SessionScoped
public class DisplayOrderBean implements Serializable {

	@Inject
	OrderBu orderBu;
	
	@ManagedProperty(value="loginMB")
	private LoginMB login;
	private double total;
	private List<OrderLine> orderLineList;
	private List<Order> orderList;

	public OrderBu getOrderBu() {
		return orderBu;
	}

	public void setOrderBu(OrderBu orderBu) {
		this.orderBu = orderBu;
	}

	public List<OrderLine> getOrderLineList() {
		
		//recuperation de la session
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
		HttpSession session = ((HttpServletRequest) request).getSession(false);
		
		//recuperation du login
		login = (session != null) ? (LoginMB) session.getAttribute("loginMB") : null;
		//recuperation du user
		System.out.println("login user from session : "+login.getUser().getIdPerson());
		orderList = orderBu.getLastOrder(login.getUser().getIdPerson());
		
		Set<OrderLine> set = new HashSet<OrderLine>();
		set = orderList.get(orderList.size()-1).getOrderLines();
		orderLineList = new ArrayList<OrderLine>(set);
		setTotal(orderList.get(orderList.size()-1).getTotalAmount());
		
		
		return orderLineList;
	}
	public String goHomePage() {
		return "home.jsf";
	}

	public void setOrderLineList(List<OrderLine> orderLineList) {
		this.orderLineList = orderLineList;
	}

	public List<Order> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}
	

	
}
